#include "Core/Log/LogManager.h"
#include "GUIManager.h"

namespace GUI
{
	namespace Win32
	{
		void GUIManager::Register(HWND _hWnd, Core::GUI::IWidget* _pWidget)
		{
			auto it = m_hWidgets.find(_hWnd);
			if (it == std::end(m_hWidgets))
			{
				m_hWidgets[_hWnd] = _pWidget;
			}
			else
			{
				LOG_ERROR("[Register] Widget with handle %d already registered", _hWnd);
			}
		}

		void GUIManager::Unregister(HWND _hWnd)
		{
			auto it = m_hWidgets.find(_hWnd);
			if (it != std::end(m_hWidgets))
			{
				m_hWidgets.erase(it);
			}
			else
			{
				LOG_ERROR("[Unregister] Unable to find handle %d", _hWnd);
			}
		}

		Core::GUI::IWidget* GUIManager::GetWidget(HWND _hWnd, Core::GUI::WidgetType _type) const
		{
			auto it = m_hWidgets.find(_hWnd);
			if (it != std::end(m_hWidgets) && it->second->GetType() == _type)
			{
				return it->second;
			}
			return NULL;
		}

		Core::GUI::IWindow * GUIManager::GetWindow(HWND _hWnd) const
		{
			return (Core::GUI::IWindow*)GetWidget(_hWnd, Core::GUI::Window);
		}

		Core::GUI::IButton * GUIManager::GetButton(HWND _hWnd) const
		{
			return (Core::GUI::IButton*)GetWidget(_hWnd, Core::GUI::Button);
		}
	}
}